<?php
    class mysql {
        private $db_host; //数据库主机
        private $port;      //数据库端口
        private $db_user; //数据库用户名
        private $db_pwd; //数据库用户名密码
        private $db_database; //数据库名
        private $conn; //数据库连接标识;
        private $result; //执行query命令的结果资源标识
        private $sql; //sql执行语句
        private $coding; //数据库编码，GBK,UTF8,gb2312
        
        /*构造函数*/
        public function __construct($db_host, $port, $db_user, $db_pwd, $db_database, $coding) {
            $this->db_host = $db_host;
            $this->port = $port;
            $this->db_user = $db_user;
            $this->db_pwd = $db_pwd;
            $this->db_database = $db_database;
            $this->conn = null;
            $this->coding = $coding;
            $this->connect();
        }
        
        /*数据库连接*/
        public function connect() {
            $this->conn = mysqli_connect($this->db_host, $this->db_user, $this->db_pwd, $this->db_database, $this->port);
            if (!$this->conn) {
                return false;
            }
            
            if (!mysqli_select_db($this->conn, $this->db_database)) {
                return false;
            }
            mysqli_query($this->conn,"SET NAMES $this->coding");
            return true;
        }
        
        /*数据库执行语句，可执行查询添加修改删除等任何sql语句*/
        public function query($sql) {
            if ($sql == "") {
                return null;
            }
            
            $this->sql = $sql;
            $result = mysqli_query($this->conn,$this->sql);
            
            if (!$result) {
                return null;
            } else {
                $this->result = $result;
            }
            return $this->result;
        }
        
        /*
         mysqli_fetch_row()    array  $row[0],$row[1],$row[2]
         mysqli_fetch_array()  array  $row[0] 或 $row[id]
         mysqli_fetch_assoc()  array  用$row->content 字段大小写敏感
         mysqli_fetch_object() object 用$row[id],$row[content] 字段大小写敏感
         */
        
        /*取得记录集,获取数组-索引和关联,使用$row['content'] */
        public function fetch_array($resultt="") {
            if($resultt<>""){
                return mysqli_fetch_array($resultt);
            }else{
                return mysqli_fetch_array($this->result);
            }
        }
        
        //获取关联数组,使用$row['字段名']
        public function fetch_assoc() {
            return mysqli_fetch_assoc($this->result);
        }
        
        //获取数字索引数组,使用$row[0],$row[1],$row[2]
        public function fetch_row() {
            return mysqli_fetch_row($this->result);
        }
        
        //获取对象数组,使用$row->content
        public function fetch_Object() {
            return mysqli_fetch_object($this->result);
        }
        
        /*取得上一步 INSERT 操作产生的 ID*/
        public function insert_id() {
            return mysqli_insert_id($this->conn);
        }
        
        // 根据select查询结果计算结果集条数
        public function db_num_rows() {
            if ($this->result == null) {
                return 0;
            } else {
                return mysqli_num_rows($this->result);
            }
        }
        
        // 根据insert,update,delete执行结果取得影响行数
        public function db_affected_rows() {
            return mysqli_affected_rows($this->conn);
        }
        
        //释放结果集
        public function free() {
            if ($this->result!=null && is_object($this->result)) {
                mysqli_free_result($this->result);
            }
        }
        
        //数据库选择
        public function select_db($db_database) {
            return mysqli_select_db($this->conn, $db_database);
        }
        
        //析构函数，自动关闭数据库,垃圾回收机制
        public function __destruct() {
            $this->free();
            if ($this->conn!=null && is_object($this->conn)) {
                mysqli_close($this->conn);
            }
        } //function __destruct();
    }

    function new_mysql() {
        global $sharedb;
        return new mysql($sharedb["host"], $sharedb["port"], $sharedb["user"], $sharedb["pswd"], $sharedb["db"], $sharedb["coding"]);
    }